import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

export const useCounterStore = defineStore('counter', () => {

  let tasklist = ref([])
  
  //新增任务
  let add = (payload)=>{
    tasklist.value.push({ id : tasklist.value.length , name:payload, status:false})
  }
  //更改任务状态
  let change = (payload)=>{ //payload是传来的任务id
    let a  =  tasklist.value.find((item)=>{
      return item.id == payload;
    })
    if( a ){
      a.status = !a.status;
      console.log(a.status)
    }
  }
  //删除任务
  let del = (payload)=>{
    let a = tasklist.value.findIndex(item=>{
      return item.id == payload
    })
    if( a != -1){
      tasklist.value.splice( a , 1)
    }
  }
  //计算未完成的状态
  var num = computed(()=>{
    return tasklist.value.filter((item)=>item.status == false).length;
  })

  return { add, change, del,num ,tasklist}
})
